package com.poker.duiued;

import android.os.Bundle;
import android.view.View;
import android.widget.*;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.poker.iuious.yhyhjs.jklimj.FeedbackResponse;
import com.poker.jkiuus.R;
import com.poker.rtyfgh.NetworkCallback;
import com.poker.jkiuus.uyrfgd.AppManager;
import com.poker.jkiuus.uyrfgd.DatabaseHelper;
import com.poker.jkiuus.uyrfgd.NetworkUtils;


public class FeedBackActivity extends AppCompatActivity {
    private Spinner feedback_type_spinner;
    private EditText feedback_content;
    private Button feedback_submit;
    private ProgressBar progressBar;
    private Toast currentToast;
    private String userName;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        AppManager.addActivity(this);

        init();


    }

    private void init(){
        feedback_type_spinner = findViewById(R.id.feedback_type_spinner);
        feedback_content = findViewById(R.id.feedback_content);
        feedback_submit = findViewById(R.id.feedback_submit);
        progressBar = findViewById(R.id.progressBar);

        if (getIntent().hasExtra("USER_NAME")) {
            userName = getIntent().getStringExtra("USER_NAME");
        }else {
            userName = "";
        }

        feedback_submit.setOnClickListener(v -> submitFeedback());
    }

    private void submitFeedback() {

        String feedbackType = feedback_type_spinner.getSelectedItem().toString();
        String feedbackContent = feedback_content.getText().toString();


        if (feedbackContent.isEmpty()) {
            if (currentToast != null) {
                currentToast.cancel();
            }

            currentToast = Toast.makeText(this, "Please enter your feedback", Toast.LENGTH_SHORT);
            currentToast.show();
            return;
        }
        if (feedbackType.isEmpty()) {
            if (currentToast != null) {
                currentToast.cancel();
            }

            currentToast = Toast.makeText(this, "Please select a feedback type", Toast.LENGTH_SHORT);
            currentToast.show();
            return;
        }

        sendFeedbackToServer(userName,feedbackType, feedbackContent);
        progressBar.setVisibility(View.VISIBLE);

        feedback_submit.setVisibility(View.GONE);
    }

    private void sendFeedbackToServer(String userName,String feedbackType, String feedbackContent) {
         NetworkUtils.postFeedback(userName,feedbackType, feedbackContent, new NetworkCallback() {
             @Override
             public void onSuccess(FeedbackResponse response) {
                 runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         if (currentToast != null) {
                             currentToast.cancel();
                         }
                         progressBar.setVisibility(View.GONE);
                         feedback_submit.setVisibility(View.VISIBLE);
                         currentToast = Toast.makeText(FeedBackActivity.this, "The feedback was submitted successfully.", Toast.LENGTH_SHORT);
                         currentToast.show();
                     }
                 });
             }

             @Override
             public void onFailure(Throwable throwable) {
                 runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         DatabaseHelper dbHelper = new DatabaseHelper(FeedBackActivity.this);
                         boolean b = dbHelper.insertFeedback(feedbackType, feedbackContent);
                         if (currentToast != null) {
                             currentToast.cancel();
                         }
                         if (b){
                             currentToast = Toast.makeText(FeedBackActivity.this, "The feedback was submitted successfully.", Toast.LENGTH_SHORT);
                         }else {
                             currentToast = Toast.makeText(FeedBackActivity.this, "Feedback submission failed, please try again.", Toast.LENGTH_SHORT);
                         }
                         progressBar.setVisibility(View.GONE);
                         feedback_submit.setVisibility(View.VISIBLE);
                         currentToast.show();
                     }
                 });
             }
         });
    }

}
